package org.example.com.pattern.action.chain_of_responsibility

/**
 * 行为型模式-责任连模式
 */

interface ComponentWithContextualHelp {
    fun showHelp()
}

abstract class Component : ComponentWithContextualHelp {
    var tooltipText: String? = null
    var container: Container? = null
    override fun showHelp() {
        if (tooltipText != null) {
            println("showHelp ${this.tooltipText}")
        } else {
            container?.showHelp()
        }
    }
}

abstract class Container : Component() {
    protected val children = mutableListOf<Component>()

    fun add(child: Component) {
        children.add(child)
        child.container = this
    }
}

class Panel : Container() {
    var modalHelpText: String? = null
    override fun showHelp() {
        if (modalHelpText != null) {
            //
            println("Panel showHelp ${modalHelpText}")
        } else {
            container?.showHelp()
        }
    }
}

class Dialog : Container() {
    var wikiPageURL: String? = null
    override fun showHelp() {
        if (wikiPageURL != null) {
            println("WikiPage URL is $wikiPageURL")
        } else {
            container?.showHelp()
        }
    }
}